/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-18     BruceOu      first implementation
 */
#ifndef __BOARD_H__
#define __BOARD_H__

#include "gd32f4xx.h"
#include <rthw.h>

#include <rtthread.h>
//#include "drv_common.h"
#include "hal_gd32_lib_cnv.h"


#include "gd32f4xx_exti.h"

#define GD32_FLASH_START_ADRESS       ((uint32_t)0x08000000)
#define GD32_FLASH_SIZE               (1024 * 1024)
#define GD32_FLASH_END_ADDRESS        ((uint32_t)(GD32_FLASH_START_ADRESS + GD32_FLASH_SIZE))

#define EXT_SDRAM_BEGIN    (0xC0000000U) /* the begining address of external SDRAM */
#define EXT_SDRAM_END      (EXT_SDRAM_BEGIN + (32U * 1024 * 1024)) /* the end address of external SDRAM */

// <o> Internal SRAM memory size[Kbytes] <8-96>
//  <i>Default: 192   
#ifdef __ICCARM__
// Use *.icf ram symbal, to avoid hardcode.
extern char __ICFEDIT_region_RAM_end__;
#define GD32_SRAM_END          &__ICFEDIT_region_RAM_end__
#else
#define GD32_SRAM_SIZE         192  //注意另外192-128kb
#define GD32_SRAM_END          (0x20000000 + GD32_SRAM_SIZE * 1024)
#endif

#ifdef __CC_ARM
extern int Image$$RW_IRAM1$$ZI$$Limit;
#define HEAP_BEGIN    (&Image$$RW_IRAM1$$ZI$$Limit)
#elif __ICCARM__
#pragma section="HEAP"
#define HEAP_BEGIN    (__segment_end("HEAP"))
#else
extern int __bss_end;
#define HEAP_BEGIN    (&__bss_end)
#endif

#define HEAP_END          GD32_SRAM_END



#ifndef WKUP_PIN
#define WKUP_PIN		GET_PIN(A, 0)
#endif

/// 设备供电使能总开关管脚，当KEY_MF按键按下后，MCU需要将此管脚拉高使能
#ifndef POWER_ON_PIN
#define POWER_ON_PIN    GET_PIN(A, 8)
#endif

#define SOFTVER 			"1.1.6"
#define HARDVER				"2.0.1"
#define HOSTID				"1234567890654321"
#define STIMULATOR_ID_NAME	"stimID"

#define POWER_TAG "POWER"

#define CONTROLLER_HARDVER	"0.0.1"
#define CONTROLLER_SOFTVER	"0.1.7"

#define WDT_DEVICE_NAME    "wdt"    /* 看门狗设备名称 */
#define WDT_DEVICE_TIMEOUT    20     /* 看门狗超时时间 */

#define DEVID_STRING_LENTH		17	///< "80:4B:50:3C:5E:7D" 即MAC地址
#define STIM_UID_LENTH			4	///<  即920M-S版本刺激器的UID长度

#ifndef STM32L476xx
#define STM32L476xx
#endif

typedef struct {
	char softVer[20];	///< 软件版本，比如"4.0.3"
	char hardVer[20];	///< 硬件版本，比如"1.0.1"
	char devId[DEVID_STRING_LENTH+1];		///< 设备ID，默认是MAC地址，比如"F0:F8:F2:26:77:8D"
	char id;			///< 蓝牙模块连接设备的对端id（包括主设备和从设备）
}device_ver_t;

typedef struct {
	char hardVer[20];	///< 硬件版本，比如"1.0.1"
	char devId[STIM_UID_LENTH];		///< 920M-S版本刺激器UID
}stim_ver_t;

/* 设备当前的状态 */
enum device_pwr_type
{
	POWER_OFF 	= 1,	///< 能控器开机
	POWER_ON 	= 2,	///< 能控器关机
};


extern void delay_us(uint32_t nus);
void feed_watchdog(void *param);
void SystemClock_Config(void);


#ifdef RT_USING_PM

void SystemClock_MSI_ON(void);
void SystemClock_MSI_OFF(void);
void SystemClock_80M(void);
void SystemClock_24M(void);
void SystemClock_2M(void);
void SystemClock_ReConfig(uint8_t mode);

#endif

#endif

